package com.lwm.service.impl;

import cn.hutool.core.util.IdUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lwm.domain.Device;
import com.lwm.domain.ResponseResult;
import com.lwm.mapper.DeviceMapper;
import com.lwm.service.DeviceService;
import com.lwm.utils.Code;
import com.lwm.utils.HintMessage;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * 设备信息业务层
 */
@Service
public class DeviceServiceImpl implements DeviceService {
    @Resource
    private DeviceMapper deviceMapper;

    // TODO 需要处理 唯一索引约束重复异常 encoding √
    //  还有软删后的数据对应的唯一字段需要处理，处理方式，
    //  如：TM1223 拼接上 - 和 id => TM123-xx... √

    /**
     * 保存
     *
     * @param device 设备
     * @return {@link ResponseResult}<{@link Object}>
     */
    @Override
    public ResponseResult<Object> save(Device device) {
        // 必填参数 设备名称
        if (!StringUtils.hasText(device.getDeviceName())) {
            return new ResponseResult<>(Code.SAVE_ERR, HintMessage.MUST_PARAM_HINT);
        }
        if (null == device.getStatus()) device.setStatus(0);// 设备状态默认值 0 空闲
        device.setId(IdUtil.getSnowflakeNextId());
        deviceMapper.insert(device);
        return new ResponseResult<>(Code.SAVE_OK, HintMessage.SAVE_OK_HINT);
    }

    /**
     * 删除
     *
     * @param ids 设备id集合
     * @return {@link ResponseResult}<{@link Object}>
     */
    @Override
    public ResponseResult<Object> remove(List<Long> ids) {
        // 没有id，给予提示
        if (ids.isEmpty()) return new ResponseResult<>(Code.DELETE_ERR, HintMessage.REMOVE_LACK_ID + "设备");
        // 根据id获取对应的数据，取得其中的encoding，进行拼接操作
        handleData(ids);

        int i = deviceMapper.deleteByIds(ids);
        if (i <= 0) {
            return new ResponseResult<>(Code.DELETE_ERR, HintMessage.REMOVE_ERR_HINT);
        }
        return new ResponseResult<>(Code.DELETE_OK, HintMessage.REMOVE_OK_HINT);
    }

    /**
     * 数据处理
     * 对于分类编码进行处理
     * 如：TM1223 拼接上 - 和 雪花id => TM123-xx...
     *
     * @param ids id集合
     */
    @Override
    public void handleData(List<Long> ids) {
        for (Long id : ids) {
            Device device = deviceMapper.selectById(id);
            // 避免空指针异常
            if (!Objects.isNull(device)) {
                device.setEncoding(device.getEncoding() + "-" + IdUtil.getSnowflakeNextId());
                // 然后修改数据
                deviceMapper.update(device);
            }
        }
    }

    /**
     * 编辑
     *
     * @param device 设备
     * @return {@link ResponseResult}<{@link Object}>
     */
    @Override
    public ResponseResult<Object> edit(Device device) {
        if (null == device.getId()) {
            return new ResponseResult<>(Code.UPDATE_ERR, HintMessage.EDIT_LACK_ID + "设备");
        }
        int i = deviceMapper.update(device);
        if (i <= 0) {
            return new ResponseResult<>(Code.UPDATE_ERR, HintMessage.EDIT_ERR_HINT);
        }
        return new ResponseResult<>(Code.UPDATE_OK, HintMessage.EDIT_OK_HINT);
    }

    /**
     * 分页
     *
     * @param page   页码
     * @param size   数量
     * @param device 设备
     * @return {@link ResponseResult}<{@link PageInfo}<{@link Device}>>
     */
    @Override
    public ResponseResult<PageInfo<Device>> page(Integer page, Integer size, Device device) {
        if (page == null) page = 1;
        if (size == null) size = 5;
        PageInfo<Device> pageInfo = PageHelper.startPage(page, size)
                .doSelectPageInfo(() -> deviceMapper.selectAll(device));
        return new ResponseResult<>(Code.GET_OK, HintMessage.GET_OK_HINT, pageInfo);
    }

    /**
     * 根据id查询
     *
     * @param id 设备id
     * @return {@link ResponseResult}<{@link Object}>
     */
    @Override
    public ResponseResult<Object> getById(Long id) {
        if (null == id) return new ResponseResult<>(Code.GET_ERR, HintMessage.GET_LACK_ID + "设备");
        Device device = deviceMapper.selectById(id);
        if (Objects.isNull(device)) {
            return new ResponseResult<>(Code.GET_ERR, HintMessage.GET_ERR_HINT);
        }
        return new ResponseResult<>(Code.GET_OK, HintMessage.GET_OK_HINT, device);
    }

    /**
     * 根据id集合查询
     *
     * @param deviceIds 设备id
     * @return {@link ResponseResult}<{@link List}<{@link Device}>>
     */
    @Override
    public ResponseResult<List<Device>> getByIds(List<Long> deviceIds) {
        List<Device> deviceList = deviceMapper.selectByIds(deviceIds);
        return new ResponseResult<>(Code.GET_OK, HintMessage.GET_OK_HINT, deviceList);
    }
}
